﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace Dungeon_Dive
{
    class XMLEngine
    {
        private XmlReader reader;
        private XmlReaderSettings readerSettings;
        private XmlWriter writer;
        private XmlWriterSettings writerSettings;

        public XMLEngine()
        {
            readerSettings = new XmlReaderSettings();
            readerSettings.IgnoreComments = true;
            readerSettings.ValidationType = ValidationType.Schema;
            readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation
                | XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints;
            readerSettings.ConformanceLevel = ConformanceLevel.Auto;
            readerSettings.IgnoreWhitespace = true;
        }

        public bool OpenFile(String xmlFile)
        {
            try
            {
                reader = XmlReader.Create(xmlFile, readerSettings);
                reader.Read(); //Read the initial XML header
            }
            catch (FileNotFoundException)
            {
                return false;
            }
            return true;
        }

        public void CloseFile()
        {
            reader.Close();
            reader = null;
        }

        public String NextElement()
        {
            try
            {
                reader.Read();
                return reader.Name;
            }
            catch (XmlException xmlEx)
            {
                return xmlEx.Message;
            }
        }

        public String GetContent()
        {
            try
            {
                if (reader.IsEmptyElement) return "EMPTY";
                if (reader.NodeType == XmlNodeType.Text)
                {
                    return reader.ReadContentAsString();
                }
                else return "Not a text node!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public String GetValue()
        {
            return reader.Value;
        }

        public String GetAttribute(String attrib)
        {
            return reader.GetAttribute(attrib);
        }

        public void CreateFile(String filename)
        {
            writerSettings = new XmlWriterSettings();
            writerSettings.Indent = true;
            writerSettings.NewLineOnAttributes = false;
            writerSettings.OmitXmlDeclaration = false;

            writer = XmlWriter.Create(filename, writerSettings);
            writer.WriteStartDocument();
        }

        public void AddComment(String comment)
        {
            if (writer != null)
            {
                writer.WriteComment(comment);
            }
        }

        public void StartElement(string elem)
        {
            if (writer != null)
            {
                writer.WriteStartElement(elem);
            }
        }

        public void EndElement()
        {
            writer.WriteEndElement();
        }

        public void AddAttribute(string attr, string value)
        {
            writer.WriteAttributeString(attr, value);
        }

        public void AddText(string text)
        {
            writer.WriteString(text);
        }

        public void EndDocument()
        {
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }
    }
}
